package com.app.controller.backstage;

import com.app.model.AdminUserModel;
import com.app.model.ConfigModel;
import com.app.service.AdminUserService;
import com.app.service.ConfigService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.ConfigVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Controller
public class ConfigBackstageController {
    @Autowired
    AdminUserService adminUserService;
    @Autowired
    ConfigService configService;



    @GetMapping("/getNewsTypeConfigList")
    public Result<Object> getNewsTypeConfigList(
            int page, int limit,
            @RequestParam(required = false) String keyWord,
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        Page<ConfigModel> configModelPage = configService.findByNewsTypePage(keyWord,page,limit);
        ConfigVo configVo = new ConfigVo();
        configVo.setList(configModelPage.getRecords());
        configVo.setCount((int) configModelPage.getTotal());
        return Result.success(configVo);
    }
    @GetMapping("/getNewsTypeFatherList")
    public Result<Object> getNewsTypeConfigList(HttpServletRequest request) {
        List<ConfigModel> configModelList = configService.getGroupListByType(6,0);
        return Result.success(configModelList);
    }
    @PostMapping("/editNewsTypeConfig")
    @ResponseBody
    public Result<Object> editNewsTypeConfig(HttpServletRequest request,ConfigModel model) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        model.setConType(6);
        configService.insert(model);
        return Result.success("操作成功");
    }

    @PostMapping("/delNewsTypeConfig")
    @ResponseBody
    public Result<Object> delNewsTypeConfig(HttpServletRequest request,Integer id) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        configService.delete(id);
        return Result.success("操作成功");
    }

}
